/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package Wires.impl;

import java.io.File;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import Wires.WiresPackage;
import Wires.WiresSpecification;

/**
 * <!-- begin-user-doc -->
 * An class to launch a <b>Wires* Model</b>.
 * <!-- end-user-doc -->
 */
public class WiresLauncher {
	
	public static String RESOURCES_LOCATION = "";

	/**
	 * Run a Wires* Model under eclipse (all the models, transformation ... 
	 * resources are located at the workspace)
	 * @param wiresModelPath
	 * @throws Exception
	 */
	public static void run(String wiresModelPath) throws Exception {		
		WiresSpecification ws = getWiresSpecification(wiresModelPath);
		((WiresSpecificationImpl)ws).runExecution();
	}

	/**
	 * Run a Wires* Model as part of a Eclipse plug-in. In this case the Wires* Model consists
	 * in one (or more) CompositeTransformationType.
	 * 
	 * For each CompositeTransformatioType argument add an entry to the 'args' Map where the key
	 * is the name of the CompositeTransformationType argument and the value is the eclipse workspace
	 * relative path of the input model for this argument.
	 * 
	 * The resources location indicates Wires* Engine where to find the CompositeTransformationType
	 * inside elements (such as transformation types, model types etc.) Usually we pack this resources
	 * into the plug-in we are developing so it could be:
	 * 
	 * URL bundleRoot = Platform.getBundle("MyPluginName").getEntry("/");  
	 * URL fileURL = FileLocator.toFileURL(bundleRoot);
	 * String resourcesPath = fileURL.getPath();
	 * resourcesPath = resorucesPath.substring(1);
	 * 
	 * The path for each resource is made by 'resourcesPath' concat the path value of the element in the model
	 * that should be the relative path for this element at 'resourcesPath' location. 
	 * 
	 * @param wiresModelPath
	 * @param args
	 * @param resourcesLocation
	 * @throws Exception
	 */
	public static void run(String wiresModelPath, Map<String, String> args, String resourcesLocation) throws Exception{
		RESOURCES_LOCATION = resourcesLocation;
		WiresSpecification ws = getWiresSpecification(wiresModelPath);
		((WiresSpecificationImpl)ws).runExecution(args);
	}
	
	
	/**
	 * Run a Wires* Model stand-alone. Each element path is its path attribute.
	 * @param wiresModelPath
	 * @param args
	 * @throws Exception 
	 */
	public static void runStandAlone(String wiresModelPath, Map<String, String> args) throws Exception{
		WiresSpecification ws = getWiresSpecification(wiresModelPath);
		((WiresSpecificationImpl)ws).runExcecutionStandAlone(args);
	}
	
	/**
	 * Run Wires* from command line.
	 * @param args
	 */
	public static void main(String args[]){
		
	}

	private static WiresSpecification getWiresSpecification(String wiresModelPath) {
		WiresSpecification ws = null;
		// Create a resource set to hold the resources.
		ResourceSet resourceSet = new ResourceSetImpl();

		// Register the appropriate resource factory to handle all file extensions.
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
		(Resource.Factory.Registry.DEFAULT_EXTENSION, 
				new XMIResourceFactoryImpl());

		// Register the package to ensure it is available during loading.
		resourceSet.getPackageRegistry().put
		(WiresPackage.eNS_URI, 
				WiresPackage.eINSTANCE);

		File file = new File(wiresModelPath);
		URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()): URI.createURI(wiresModelPath);
		Resource resource = resourceSet.getResource(uri, true);	

		ws =  (WiresSpecification) resource.getContents().get(0);
		return ws;
	}

}
